package servlet;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import business.BusinessUser;
import domain.User;

/**
 * Servlet implementation class FronControllerLogin
 */
@WebServlet(description = "Servlet para Login", urlPatterns = { "/FronControllerLogin" })
public class FrontControllerLogin extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public FrontControllerLogin() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		loginRequest(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		loginRequest(request, response);
	}
	
	
	public void loginRequest(HttpServletRequest request,
			HttpServletResponse response) throws IOException, ServletException {
		
		String accion=request.getParameter("accion");
		if(accion.equals("login"))
		{
			String user=request.getParameter("login");
			String pass=request.getParameter("pass");
			User usuario=BusinessUser.getUserByLogin(user);
			if(usuario!=null)
			{	
				if(user.equals(usuario.getLogin()))
				{	
					if(pass.equals(usuario.getPassword()))
					{
						HttpSession session=request.getSession();
						session.setAttribute("session.user", usuario);
						RequestDispatcher d = request.getRequestDispatcher("wall.jsp");
						if(d!=null){
							d.forward(request,response);
						}
					}
					else
					{
						response.sendRedirect("error.html");
					}
				}
				else
				{
					response.sendRedirect("error.html");
				}
			}
			else
			{
				response.sendRedirect("error.html");
			}
		}
		else if(accion.equals("logout"))
		{
			request.getSession().invalidate();
			response.sendRedirect("index.jsp");
		}
		else if(accion.equals("register"))
		{
			User registro = new User(request.getParameter("nombre"),request.getParameter("apellidos"),request.getParameter("login"),request.getParameter("password"),request.getParameter("sexo"),request.getParameter("ciudad"),request.getParameter("provincia"),request.getParameter("pais"),request.getParameter("email"),new Integer(request.getParameter("anioNac")),null,request.getParameter("actFav"),request.getParameter("genFav"));
			if(BusinessUser.createAcount(registro))
			{
				request.setAttribute("ErrorRegistro", "false");
				request.getRequestDispatcher("createAcount.jsp").forward(request, response);
			}
			else
			{
				request.setAttribute("ErrorRegistro", "true");
				request.getRequestDispatcher("createAcount.jsp").forward(request, response);
			}
		}
		else if(accion.equals("addFriend"))
		{
			Integer idUserAdd =new Integer(request.getParameter("idUserAdd"));
			HttpSession session = request.getSession();
			User usuario=(User)session.getAttribute("session.user");
			if(BusinessUser.addFriend(usuario.getIdUser(),idUserAdd))
			{
				User userRecargado=BusinessUser.getUserByLogin(usuario.getLogin());
				session.setAttribute("session.user",userRecargado);
				request.setAttribute("ErrorAddFriend", "false");
				request.getRequestDispatcher("/friendProfile.jsp?idUser="+idUserAdd).forward(request, response);
				
			}
			else
			{
				request.setAttribute("ErrorAddFriend", "true");
				request.getRequestDispatcher("/friendProfile.jsp?idUser="+idUserAdd).forward(request, response);
			}
		}
	}

}
